
//路由鉴权

import {router} from './index'
import {localGetUserLogin, localSetUserLogin, useUserStore} from "../store/index.js";
import {Message} from "@arco-design/web-vue";
import {getCurrentUser} from "../apis/UserApi.js";


//路由前置 权限检查
router.beforeEach(async (to, from, next) => {
    const store = useUserStore()
    //只放行 登录页面与做问卷页面 与 首页
    if (to.path === '/login' || to.path.startsWith('/preview')
        || to.path.startsWith('/result')) {
        return next()
    } else {
        if (localGetUserLogin()) {
            if (!store.userinfo.id) {
                const res = await getCurrentUser()
            }
            if (to.path === '/index') {
                return next()
            }

            if (to.meta && to.meta.role === 'admin') {
                if (store.userinfo.userRole !== 'admin') {
                    Message.error('您无权限访问')
                    return next('/index')
                }
            }
            return next()
        } else {
            if (to.path === '/index') {
                return next()
            }
            Message.info('请先登录')
            localSetUserLogin(false)
            //重定向登录页面 携带跳转地址
            return next('/login?redirect=' + to.path)
        }
    }
})

